import os
import re
import urllib
import requests
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
res = requests.get("https://www.nowcoder.com/tutorial/10029/ecbe986667be4d738956f3e105aebe7f")
res.encoding = "utf-8"
print(res.text)
# 获取标签中的内容
# 标签中无参数如 <title> <h> <p> 一般未加属性
content1 = re.findall(r'<title>(.*?)</title>', res.text)
content2 = re.findall(r'<p>(.*?)</p >', res.text)
print(content1)
print(content2)

# 标签中含有参数 < a href=""> </ a>
# <a.*?>(.*?)</ a>  .*?略去  完整超链接<a .*? href= >
content3 = re.findall(r'<a.*?>(.*?)</ a>', res.text)
print(content3)

# 爬取参数

# 爬取图片的地址
content4 = re.findall(r'src="(.*?)"', res.text)
print(content4)
list = []
for item in content4:
  if(item[0]=='h'):
    list.append(item)
print(list)
i = 1
# 进行地址字符串的单独进行请求写入
for item in list:
  print(item)
  res = requests.get(item)
  # open(filename, 'wb') 进行创建文件
  with open("D:/python/第二天/image/"+str(i)+".jpg", 'wb') as f:
    f.write(res.content)
  i=i+1